package com.oscar.ehome.view.pageview;

import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.joanzapata.pdfview.PDFView;
import com.joanzapata.pdfview.listener.OnDrawListener;
import com.joanzapata.pdfview.listener.OnLoadCompleteListener;
import com.joanzapata.pdfview.listener.OnPageChangeListener;
import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.presenter.BasePresenter;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/9/21
 */

public class ClassRoomPdfActivity extends BasesActivity implements OnPageChangeListener
        , OnLoadCompleteListener, OnDrawListener {

    /**
     *
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     *
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     *
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     *
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    /**
     *
     */
    @BindView(R.id.classroompdfView)
    PDFView classroompdfView;

    String url = "";
    int pageNumber = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classroompdf);
        ButterKnife.bind(this);
        setColor();
        initView();
        displayFromFile1(url, "pdf");
        showLoading();
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    /**
     * 设置颜色
     */
    public void setColor() {
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(ClassRoomPdfActivity.this).getSystemConfig().getColor()));
    }

    public void initView() {
        url = getIntent().getStringExtra("url");
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleTopbar.setText("详情");
    }

    /**
     * 获取打开网络的pdf文件
     *
     * @param fileUrl
     * @param fileName
     */
    private void displayFromFile1(String fileUrl, String fileName) {
        classroompdfView.fileFromLocalStorage(this, this, this, fileUrl, pageNumber, fileName);   //设置pdf文件地址

    }

    @OnClick(R.id.title_layout_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onLayerDrawn(Canvas canvas, float pageWidth, float pageHeight, int displayedPage) {

    }

    @Override
    public void loadComplete(int nbPages) {
        dismissLoading();
        Toast.makeText(ClassRoomPdfActivity.this, "加载完成", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPageChanged(int page, int pageCount) {

        ToastUtil.show(ClassRoomPdfActivity.this, "page" + page + "pagecount" + pageCount);
    }
}
